//
//  LoanFeeFootView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanFeeFootViewDelegate {
    @objc optional func customMonth() //自定义月份
    @objc optional func inputLoanMonth() //输入贷款月份
}

class LoanFeeFootView: UIButton {
    
    weak var delegate : LoanFeeFootViewDelegate?

    let monthTextField = UITextField.init(Width: 5)
    let interestLabel = UILabel() //利息
    let supplyLabel = UILabel() //月供
    
    init(frame:CGRect,model:GenericLoansModel) {
        super.init(frame: frame)
        
        monthTextField.placeholder = customMonth
        monthTextField.keyboardType = .numberPad
        monthTextField.textAlignment = .center
        monthTextField.font = UIFont.boldSystemFont(ofSize: 16)
        monthTextField.layer.cornerRadius = RATIO_H(maxNum: 20)
        monthTextField.layer.masksToBounds = true
        monthTextField.layer.borderColor = UIColor.lineColor().cgColor
        monthTextField.layer.borderWidth = 1
        monthTextField.delegate = self
        monthTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
        self.addSubview(monthTextField)
        
        interestLabel.text = "总利息¥0.00元"
        interestLabel.textAlignment = .right
        interestLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        self.addSubview(interestLabel)
        
        supplyLabel.text = "月供0.00元"
        supplyLabel.textColor = UIColor.gray
        supplyLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(supplyLabel)
        
        monthTextField.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 40))
            make.width.equalTo(RATIO(maxNum: ScreenW/3))
        }
        
        interestLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalTo(monthTextField)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        supplyLabel.snp.makeConstraints { (make) in
            make.top.equalTo(interestLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.left.right.equalTo(interestLabel)
            make.width.greaterThanOrEqualTo(10)
        }
    }
    
    //MARK: 输入月份
    @objc fileprivate func textFieldDidChange(textField:UITextField) {
        
        if textField.text == "0" {
            return
        }
        
        if Double(textField.text!) ?? 0 > 36 {
            textField.text = "36"
            KMTool.showText(_text: maxLoanMonth)
        }
        
        self.delegate?.inputLoanMonth!()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}

extension LoanFeeFootView : UITextFieldDelegate {
    
    //MARK: 点击输入框
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        self.delegate?.customMonth!()
    }
    
    //MARK: 输入限制
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if textField.text == "0" {
            if range.length == 1 && string.count == 0 {
                return true
            }else{
                return false
            }
        }
        
      
        if (textField.text?.count)! >= 2 {
            
            if range.length == 1 && string.count == 0 {
                return true
            }else{
                return false
            }
        }
        
        return true
    }
    
}
